<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <!-- <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.cityworkDelay_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template> -->
       <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handleReceive(scope.row,scope.index)"
          v-if="scope.row.status ==0"
        >接收/拒绝
        </el-button>
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handleAgree(scope.row,scope.index)"
          v-if="scope.row.status ==1"
        >同意/拒绝
        </el-button>
        <el-button
          type="text"
          icon="el-icon-circle-plus-outline"
          size="small"
          @click.stop="handleView(scope.row,scope.index)"
        >处理记录
        </el-button>
      </template>
    </avue-crud>
    <el-drawer :title="处理记录" :visible.sync="drawerVisible" :direction="direction"
               append-to-body
               :before-close="handleDrawerClose" size="1000px">
      <avue-crud :option="optionScope"
                 :data="dataScope"
                 :page="pageScope"
                 v-model="formScope"
                 ref="crudScope"
               
                 :before-open="beforeOpenScope"
                 @search-change="searchChangeScope"
                 @search-reset="searchResetScope"
                 @selection-change="selectionChangeScope"
                 @current-change="currentChangeScope"
                 @size-change="sizeChangeScope"
                 @on-load="onLoadScope">
      </avue-crud>
    </el-drawer>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,getListViewScope} from "@/api/govsvc/cityworkDelay";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {
          delstatus:1
        },
        query: {},
        loading: true,
         drawerVisible: false,
         scopeLoading: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          indexLabel:'序号',
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 750,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: false,
          excelBtn: false,
          dialogClickModal: false,
          column: [
            {
              label: "主键id",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: false,
            },
            {
              label: "工单号",
              prop: "workId",
              type: "input",
              span: 12,
              search: true,
              hide: false,
              detail: true,
            },
            {
              label: "延期至",
              prop: "delayToDate",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              hide: false,
              span: 12,
              detail: true,
              rules: [{
                required: true,
                message: "请输入延期至时间",
                trigger: "blur"
              }],
            },
            {
              label: "延期理由",
              prop: "delayReason",
              type: "textarea",
              span: 24,
              hide: false,
              overHidden: true,
              detail: true,
              rules: [{
                required: true,
                message: "请输入延期理由",
                trigger: "blur"
              }],
            },
           
            {
              label: "延期类型",
              prop: "delayType",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=delay_reason",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              span: 12,
              hide: true,
              detail: true,
              rules: [{
                required: true,
                message: "请输入延期类型",
                trigger: "blur"
              }],
            },
            {
              label: "联系人",
              prop: "concatName",
              type: "input",
              hide: true,
              detail: true,
              span: 12,
            },
            {
              label: "联系电话",
              prop: "concatPhone",
              type: "input",
              hide: true,
              detail: true,
              span: 12,
            },
             {
              label: "处理",
              prop: "delstatus",
              type: "radio",
              hide: true,
              span: 24,
              dicData: [],
              rules: [{
                required: true,
                message: "请选择处理意见",
                trigger: "blur"
              }],
            },
            {
              label: "办理情况",
              prop: "satiuation",
              type: "textarea",
              hide: true,
              detail: true,
              span: 24,
            },
            {
              label: "处理状态",
              prop: "status",
              type: "select",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: false,
               dicData: [
                {
                  label: "发起",
                  value: 0
                },
                {
                  label: "接收",
                  value: 1
                },
                {
                  label: "同意",
                  value: 2
                },
                {
                  label: "拒绝",
                  value: 3
                }
              ],
            },
           
            {
              label: "说明",
              prop: "remark",
              type: "textarea",
              span: 24,
              hide: true,
              rules: [{
                required: true,
                message: "请输入说明信息",
                trigger: "blur"
              }],
            },
            
          ]
        },
        data: [],
        formScope: {},
        selectionListScope: [],
        pageScope: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        optionScope: {
          tip: false,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          excelBtn: false,
          columnBtn: false,
          menu: false,
          refreshBtn: false,
          menuWidth: 200,
          dialogHeight: 610,
          column: [
            {
              label: "主键id",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
            {
              label: "申请记录id",
              prop: "delayId",
              type: "input",
            },
            {
              label: "操作",
              prop: "operation",
              type: "select",
               dicData: [
                {
                  label: "发起",
                  value: 0
                },
                {
                  label: "接收",
                  value: 1
                },
                {
                  label: "同意",
                  value: 2
                },
                {
                  label: "拒绝",
                  value: 3
                }
               ]
            },
            {
              label: "说明",
              prop: "remark",
              type: "input",
               overHidden: true,
            },
           
            {
              label: "操作时间",
              prop: "updateTime",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
          
          ]
        },
        dataScope: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.cityworkDelay_add, false),
          viewBtn: this.vaildData(this.permission.cityworkDelay_view, false),
          delBtn: this.vaildData(this.permission.cityworkDelay_delete, false),
          editBtn: this.vaildData(this.permission.cityworkDelay_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //处理接收
       handleReceive(row) {
          getDetail(row.id).then(res => {
            this.form = res.data.data;
          });
          var delstatus =this.findObject(this.option.column,'delstatus')
           delstatus.dicData= [               
                {
                  label: "接收",
                  value: 1
                },
                {
                  label: "拒绝",
                  value: 3
                },
                           
              ],
        this.$refs.crud.rowAdd();
      },
       //处理同意
       handleAgree(row) {
          getDetail(row.id).then(res => {
            this.form = res.data.data;
          });
          var delstatus =this.findObject(this.option.column,'delstatus')
           delstatus.dicData= [               
                {
                  label: "同意",
                  value: 2
                },
                {
                  label: "拒绝",
                  value: 3
                },
                           
              ],
        this.$refs.crud.rowAdd();
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
  // 处理记录
      handleView(row) {
        console.log(row.id)
        this.drawerVisible = true;
        this.delayId = row.id
        this.onLoadScope(this.pageScope)
      },
      handleDrawerClose(hide) {
        hide();
      },
      beforeOpenScope(done) {
        done();
      },
      searchResetScope() {
        this.onLoadScope(this.pageScope);
      },
      searchChangeScope(params) {
        this.onLoadScope(this.pageScope, params);
      },
      selectionChangeScope(list) {
        this.selectionListScope = list;
      },
      currentChangeScope(currentPage) {
        this.pageScope.currentPage = currentPage;
      },
      sizeChangeScope(pageSize) {
        this.pageScope.pageSize = pageSize;
      },
      onLoadScope(page, params = {}) {
        const values = {
          ...params,
          delayId: this.delayId,
        }
        getListViewScope(page.currentPage, page.pageSize, Object.assign(values, this.query)).then(res => {
          const data = res.data.data;
          this.pageScope.total = data.total;
          this.dataScope = data.records;
          this.selectionListScope = [];
        });
      }
    }
  };
</script>

<style>
</style>
